package com.app.view.manager 
{
	import org.puremvc.as3.interfaces.IMediator;
	import org.puremvc.as3.interfaces.INotification;
	import org.puremvc.as3.patterns.mediator.Mediator;
	
	import flash.utils.Dictionary;
	import com.peachcastle.interfaces.IScene;
	import com.app.view.component.scene.HallScene;
	import com.app.view.component.scene.RoomScene;
	
	import com.app.view.component.HallMediator;
	import com.app.view.component.RoomMediator;
	import com.app.view.manager.LayerManagerMediator;

	public class SceneManagerMediator extends Mediator implements IMediator
	{
		public static const NAME:String 					= "SceneManagerMediator";
		public static const JOIN_SCENE:String 				= NAME + '_JoinScene';
		public static const PRE_LOAD:String 				= NAME + '_PreLoad';
		public static const LEAVE_PRE_SCENE:String 			= NAME + '_LeavePreScene';	//离开前一个场景
		
		public static const CURRENT_SCENE_REQUEST:String 	= NAME + '_CurrentSceneRequest';
		public static const CURRENT_SCENE:String    	 	= NAME + '_CurrentScene';
		
		private var _scenes:Dictionary = null;
		private var _currentScene:IScene = null;
		private var _swfUrlFunc:Function = null;
		
		public function SceneManagerMediator() 
		{
			super(NAME);
			
			_scenes = new Dictionary();
		}
		
		override public function listNotificationInterests():Array
		{
			return [
				SceneManagerMediator.JOIN_SCENE,
				SceneManagerMediator.PRE_LOAD,
				SceneManagerMediator.LEAVE_PRE_SCENE,
				SceneManagerMediator.CURRENT_SCENE_REQUEST
			];
		}	
		
		override public function handleNotification(notification:INotification):void
		{
			var name:String = notification.getName();
			var param:* = notification.getBody();
			switch ( name )
			{
				case SceneManagerMediator.JOIN_SCENE:
					join(param[0], param[1]);
			
					break;
					
				case SceneManagerMediator.PRE_LOAD:
					preLoader(param[0]);
			
					break;
					
				case SceneManagerMediator.LEAVE_PRE_SCENE:
					if (preScene) preScene.leaveScene();
			
					break;
					
				case SceneManagerMediator.CURRENT_SCENE_REQUEST:
					if (currentScene)
						sendNotification(SceneManagerMediator.CURRENT_SCENE, [currentScene]);
			
					break;
			}
		}		
		
		/**
		 * 进入某个场景;
		 * @param	sname
		 */
		private function join(sname:String, swfUrlFunc:Function):void
		{
			_swfUrlFunc = swfUrlFunc;
			currentScene = getNameByScene(sname);
		}
		
		/**
		 * 预加载某个场景的资源;
		 * @param	sname
		 */
		public function preLoader(sname:String):void
		{
			var scene:IScene = getNameByScene(sname);
			scene.preLoader();
		}
		
		public function getNameByScene(sname:String):IScene
		{
			if (!_scenes[sname])
			{
				switch(sname)
				{
					case HallScene.NAME:
						_scenes[sname] = new HallScene(_swfUrlFunc);
						facade.registerMediator( new HallMediator(_scenes[sname]) );
						
						break;
					
					case RoomScene.NAME:
						_scenes[sname] = new RoomScene(_swfUrlFunc);
						facade.registerMediator( new RoomMediator(_scenes[sname]) );
						
						break;
				}
			}
			
			return _scenes[sname];
		}
		
		private var _preScene:IScene = null;	//第二个场景
		public function get preScene():IScene { return _preScene; }
		
		/**
		 * 当前场景;
		 */
		public function get currentScene():IScene { return _currentScene; }
		public function set currentScene(value:IScene):void
		{
			if (_currentScene == value) return;
			
			if (_currentScene != _preScene)
			{
				_preScene = _currentScene;
			}
			
			_currentScene = value;

			_currentScene.joinScene();
		}
	}
}